package com.cwt.service.service.impl;

import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwt.common.enums.BusinessErrorCode;
import com.cwt.common.enums.RoleEnum;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.DateUtils;
import com.cwt.common.utils.MD5Utils;
import com.cwt.common.utils.MapUtils;
import com.cwt.common.utils.StringUtils;
import com.cwt.service.domain.SysUser;
import com.cwt.service.domain.vo.MenuTreeVo;
import com.cwt.service.service.ShopCarService;
import com.cwt.service.service.ShopOrderService;
import com.cwt.service.service.SysMenuService;
import com.cwt.service.service.SysUserService;
import com.cwt.service.mapper.SysUserMapper;
import com.cwt.service.vo.Login;
import com.cwt.service.vo.Register;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author Acer
 * @description 针对表【sys_user(系统用户表)】的数据库操作Service实现
 * @createDate 2023-07-19 11:54:49
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private ShopCarService carService;

    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public AjaxResult login(Login login, String type) {
        SysUser user = baseMapper.selectByName(login.getUsername());
        if (ObjectUtils.isEmpty(user) || StringUtils.isEmpty(user.getPassword())) {
            return AjaxResult.error(BusinessErrorCode.USER_PHONE_ERROR); // 账号或者密码错误
        }

        // 密码校验
        if (!user.getPassword().equals(MD5Utils.string2MD5(login.getPassword() + user.getSalt()))) {
            return AjaxResult.error(BusinessErrorCode.USER_PHONE_ERROR); // 账号或者密码错误
        }
//        if ("manage".equals(type)) {
//            if (!RoleEnum.MANAGE.getCode().equals(baseMapper.selectUserRoleName(user.getId()))) {
//                return AjaxResult.error(BusinessErrorCode.NOT_PERMISSION_ERROR);
//            }
//        }

        // 生成 token
        SaLoginModel model = SaLoginConfig.setExtraData(MapUtils.objectToMap(user));

        // 使用 Sa-token 进行登录
        StpUtil.login(user.getId(), model);
        login.setId(user.getId());
        login.setToken(StpUtil.getTokenValue());
        login.setNickname(user.getNickname());
        login.setLogged(new Date());

        // 存储用户数据
        StpUtil.getSession().set("user-" + user.getId(), login);

        // 更新登录时间
        baseMapper.updateLastLoginTimeById(user.getId(), new Date());

        if ("manage".equals(type)) {
            // 处理管理端用户 菜单权限
            login.setMenus(sysMenuService.selectMenuTree(user.getId()));
        }
        return AjaxResult.success(login);
    }

    @Override
    public AjaxResult adminRegister(Register register) {
        String confirmPwd = register.getConfirmPwd();
        String password = register.getPassword();

        if (!password.equals(confirmPwd)) {
            return AjaxResult.error(BusinessErrorCode.TWO_TIME_PWD_ERROR);
        }
        String username = register.getUsername();
        SysUser user = baseMapper.selectByName(username);
        if (!ObjectUtils.isEmpty(user)) {
            return AjaxResult.error(BusinessErrorCode.PHONE_EXIST_ERROR);
        }
        //设置登录密码
        String salt = StringUtils.getComplexRandomString(16);
        String enPassword = MD5Utils.string2MD5(password + salt);
        SysUser userSave = SysUser.builder()
                .name(register.getName())
                .password(enPassword)
                .salt(salt)
                .build();
        baseMapper.insert(userSave);
        return AjaxResult.success("用户【" + userSave.getName() + "】新增成功");
    }

}




